package pack08_covid_grouping;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author lilulu
 * @date 2023/2/11 14:23
 */
public class CovidGroupingBean implements WritableComparable<CovidGroupingBean> {

    private String state;
    private int cases;

    @Override
    public String toString() {
        return "CovidGroupingBean{" +
                "state='" + state + '\'' +
                ", cases=" + cases +
                '}';
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public int getCases() {
        return cases;
    }

    public void setCases(int cases) {
        this.cases = cases;
    }

    public CovidGroupingBean() {
    }

    public CovidGroupingBean(String state, int cases) {
        this.state = state;
        this.cases = cases;
    }

    //指定排序规则，我们是求确诊病例最高的信息，所以必须对K2的确诊病例数进行排序
    //同一个州的确诊病例数才有必要比较
    //判断是否一个同一个州
    //如果州相同，则比较确诊病例数
    @Override
    public int compareTo(CovidGroupingBean o) {
        int result = this.state.compareTo(o.getState());
        if (result == 0) {
            return (this.cases - o.getCases())*-1;
        }
        return result;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(state);
        dataOutput.writeInt(cases);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.state = dataInput.readUTF();
        this.cases = dataInput.readInt();
    }
}
